const mongoose = require('mongoose');

const commentSchema = new mongoose.Schema({
  content: {
    type: String,
    required: [true, '评论内容不能为空']
  },
  // 评论目标类型：notice=通知, topic=话题, activity=活动, market=商品, post=帖子
  targetType: {
    type: String,
    required: true,
    enum: ['notice', 'topic', 'activity', 'market', 'post']
  },
  // 评论目标ID
  targetId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    refPath: 'targetModel'
  },
  // 根据 targetType 动态确定关联的模型
  targetModel: {
    type: String,
    required: true,
    enum: ['CampusNotice', 'HotTopic', 'Activity', 'MarketItem', 'Post']
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: [true, '评论作者不能为空']
  },
  // 父评论ID（用于回复功能）
  parentId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Comment',
    default: null
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('Comment', commentSchema);